/*
 * @Author: 詹真琦(legendryztachi@gmail.com)
 * @Date: 2022-01-17 17:55:41
 * @LastEditors: 詹真琦(legendryztachi@gmail.com)
 * @LastEditTime: 2022-05-09 13:51:27
 * @FilePath: \code\cwp-front\pc-collaboration\src\store\modules\summary\comment.js
 * @Description: 处理页面右侧数据
 */
import { defineStore, mapActions } from 'pinia';
import pinia from '@/store';
import { useBaseInfoStore } from '@/store/modules/base-info';

export const useSummaryCommentStore = defineStore('summaryComment', {
  state: () => ({
    attitudeList: [], //态度列表
    attitude: '', //默认态度
    commentText: '', //评论正文
    attachments: [], //附件
    processLogs: {}, // 流程日志
    relationDoc: [], //关联文档
    //是否点赞
    isPraise: false,
    //是否收起
    isCollapse: true,
    //是否是富文本
    isRichText: false,
    //去掉富文本后的文本内容
    textContent: '',
    //是否隐藏意见
    isHideOpinion: false,
    //对那些人展示隐藏意见
    hideOpinionObject: {
      //0.1绝密、0.2绝密、0.3绝密、1.1秘密、0.4
      text: '',
      //Member|-8067040239322578063,Member|-7716299150402043535
      value: ''
    },
    //跟踪
    trackSet: {
      //是否开启跟踪
      isTrack: false,
      //是否跟踪全部人
      isTrackAll: true,
      //跟踪人员列表
      trackMembers: '',
      //跟踪人员名称列表
      trackMemberNames: ''
    },
    //是否处理后归档
    isPigeonhole: false,
    //多部门、角色列表。如果是空数组代表就只有一个部门
    dealDepartmentList: [],
    //选择的哪一个部门
    dealDepartmentIndex: 0,
    commonPhrasesList: [], //常用语
  }),
  methods: {
    ...mapActions(useBaseInfoStore, ['setTrackData'])
  },
  actions: {
    //设置数据
    initData({ draftComment, trackData }) {
      if (draftComment) {
        const {
          extAtt4: attitude,
          content='',
          attFileDomain,
          assDocDomain,
          praiseToSummary,
          isRichContent,
          hidden,
          showToId,
          showToIdText
        } = draftComment;

        //有保存默认态度才设置
        if (attitude) {
          this.attitude = attitude;
        }
        //意见
        this.commentText = content;
        //附件、关联文档
        this.attachments = attFileDomain || [];
        this.relationDoc = assDocDomain || [];
        //是否点赞
        this.isPraise = praiseToSummary;
        //是否是富文本
        this.isRichText = isRichContent || false;
        //意见隐藏
        this.isHideOpinion = hidden || false;
        //对那些人展示隐藏意见
        this.hideOpinionObject = {
          //0.1绝密、0.2绝密、0.3绝密、1.1秘密、0.4
          text: showToIdText,
          //Member|-8067040239322578063,Member|-7716299150402043535
          value: showToId || ''
        };
      }
      //跟踪
      if (trackData) {
        this.trackSet = {
          ...this.trackSet,
          ...trackData
        };
      } else {
        useBaseInfoStore().setTrackData(this.trackSet);
      }
    },

    //设置态度列表
    setAttitudeList(attitudeList) {
      this.attitudeList = attitudeList;
      //设置默认态度。没有设置（草稿），才设置
      if (!this.attitude) {
        this.attitude = attitudeList.find((d) => d.isDefault).code;
      }
    },

    //设置部门列表
    setDealDepartmentList(dealDepartmentList) {
      if (!dealDepartmentList.length) return;
      this.dealDepartmentList = dealDepartmentList;
      //设置默认部门。
      this.dealDepartmentIndex = dealDepartmentList.findIndex((d) => d.default);
    }
  }
});

//在vue上下文之外使用
export function useSummaryCommentWithOut() {
  return useSummaryCommentStore(pinia);
}
